/**
 * 
 */
package org.wattdepot.cli.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.junit.Test;

/**
 * Tests the carboncontent command.
 * @author George Lee
 *
 */
public class TestCarbonContentCommand extends CommandTestBed {

  /** 
   * Tests the doCommand method of the carboncontent command handler.
   * 
   * @see org.wattdepot.cli.command.CommandTestBed#testDoCommand()
   */
  @Override
  @Test
  public void testDoCommand() {
    CarbonContentCommand handler = new CarbonContentCommand(this.client);
    
    //Invocation: carboncontent SIM_IPP 2009-11-15T12:00:00.000-10:00 sampling-interval 10
    //Sad tests.
    String command = "carboncontent SIM_IPP 2009-11-15 sampling-interval 10 foo";
    String result = handler.doCommand(command);
    assertTrue("Testing invalid number of parameters.",
        result.contains(WattDepotCliCommand.PARAMETER_COUNT_ERROR));
    
    command = "foo SIM_IPP 2009-11-15 sampling-interval 10";
    result = handler.doCommand(command);
    assertTrue("Testing invalid first parameter.",
        result.contains(WattDepotCliCommand.reportUnknownCommand(command)));
    
    command = "carboncontent SIM_IPP 2009-11-15 interval 10";
    result = handler.doCommand(command);
    assertTrue("Testing invalid fourth parameter",
        result.contains(WattDepotCliCommand.PARAMETER_ERROR));
    
    command = "carboncontent SIM_IPP foo sampling-interval 10";
    result = handler.doCommand(command);
    assertTrue("Testing invalid time parameter",
        result.contains(WattDepotCliCommand.TSTAMP_ERROR));
    
    command = "carboncontent SIM_IPP 2009-11-15 sampling-interval foo";
    result = handler.doCommand(command);
    assertTrue("Testing invalid interval parameter",
        result.contains(WattDepotCliCommand.INTERVAL_ERROR));
    
    command = "carboncontent SIM_IPP 2009-11-15 sampling-interval 0";
    result = handler.doCommand(command);
    assertTrue("Testing invalid value for interval",
        result.contains(WattDepotCliCommand.RANGE_ERROR));
    
    command = "carboncontent foo 2009-11-15 sampling-interval 10";
    result = handler.doCommand(command);
    assertTrue("Testing invalid value for source",
        result.contains(WattDepotCliCommand.SOURCE_ERROR));
    
    //Happy tests
    command = "carboncontent SIM_IPP 2009-11-15 sampling-interval 10";
    result = handler.doCommand(command);
    StringTokenizer tokenizer = new StringTokenizer(result);
    List<String> tokens = new ArrayList<String>();
    while (tokenizer.hasMoreTokens()) {
      tokens.add(tokenizer.nextToken());
    }
    
    try {
      double content = Double.parseDouble(tokens.get(0));
      assertTrue("Checking content value.", content >= 0.0);
    }
    catch (NumberFormatException e) {
      fail("Got bad result from carboncontent operation.");
    }
    
    assertEquals("Checking second token in result.", tokens.get(1), "lbs");
    assertEquals("Checking third token in result.", tokens.get(2), "CO2/MWh");
  }

}
